﻿using AntDesign;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System.Threading.Tasks;
using YwhLib;
using YwhWeb.Models;
using YwhWeb.Services;

namespace YwhWeb.Pages.Daily
{
    public partial class BuildingView
    {
        private CurrentUser _currentUser = new CurrentUser();

        [Inject] protected IUserService UserService { get; set; }

        private void HandleFinish()
        {
        }

        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            this.buildingRsp= YwhCore.BuildingService.List(_pageIndex, _pageSize);
            this.phaseOptions = YwhCore.PhaseService.List(1, 100).Items;
            _currentUser = await UserService.GetCurrentUserAsync();
        }

        public YwhLib.QueryRsp<YwhLib.Building> buildingRsp = new YwhLib.QueryRsp<YwhLib.Building>();

        IEnumerable<YwhLib.Building> selectedRows;
        ITable table;

        int _pageIndex = 1;
        int _pageSize = 10;
        int _total = 0;

        bool drawerVisible = false;

        void NewDrawer()
        {
            this.editBuilding = new YwhLib.Building();
            this.drawerVisible = true;
        }

        public async Task OnChange(AntDesign.TableModels.QueryModel<YwhLib.Building> queryModel)
        {
            //Console.WriteLine(JsonSerializer.Serialize(queryModel));
        }

        private YwhLib.Building editBuilding = new YwhLib.Building();

        Phase phaseSelect = null;

        List<Phase> phaseOptions = new List<Phase>();
        private void OnSelectedItemChangedHandler(Phase value)
        {
            phaseSelect = value;
        }

        private async Task DeleteBuilding()
        {
            YwhCore.BuildingService.Delete(editBuilding);
            this.buildingRsp = YwhCore.BuildingService.List(_pageIndex, _pageSize);
            base.StateHasChanged();
        }

        private async Task UpdateBuilding()
        {
            this.drawerVisible = true;
            YwhCore.BuildingService.Update(editBuilding);
            this.buildingRsp = YwhCore.BuildingService.List(_pageIndex, _pageSize);
            base.StateHasChanged();
        }

        private async Task SaveBuilding()
        {
            YwhCore.BuildingService.Save(editBuilding);
            this.drawerVisible = false;
            this.buildingRsp = YwhCore.BuildingService.List(_pageIndex, _pageSize);
            base.StateHasChanged();
        }
    }
}
